using System.Collections;
using System.Html;
using System.Runtime.CompilerServices;
using Awaiter;
using Nsb.Classes;
using jQueryApi;

namespace MetaPages.Classes
{
    [IgnoreNamespace]
    public class CssInfo : Actor
    {
        private const string Abouters = "body *:not(.MetaControl)";
        private static CssInfo _instance;

        private static readonly string[] CssProps = new string[]
                                                        {
                                                            "position",
                                                            "top",
                                                            "bottom",
                                                            "left",
                                                            "right",
                                                            "margin-top",
                                                            "margin-left",
                                                            "margin-right",
                                                            "margin-bottom",
                                                            "padding-top",
                                                            "padding-left",
                                                            "padding-right",
                                                            "padding-bottom",
                                                            "z-index"
                                                        };

        internal jQueryObject Btn;
        private jQueryObject _about;

        public CssInfo()
        {
            Element = jQuery.FromHtml("<div class='CssInfo MetaControl'/>")
                .AppendTo(Document.Body)
                .Hide()
                .Bind("keydown.esc", delegate(jQueryEvent e) { Keys.EscKey(e, delegate { Destroy(); }); })
                ;

            jQuery.FromObject(Abouters)
                .Live("mouseover.CssInfo", MouseEnter)
                ;

            jQuery.FromObject(Document.Body)
                .Bind("mousemove.CssInfo",
                      delegate(jQueryEvent e)
                      {
                          if (Element != null)
                              Element.CSS(new Dictionary("top", e.PageY + 34, "left", e.PageX - 80));
                      })
                ;

        }

        public void Destroy()
        {
            _instance = null;

            jQuery.FromObject(Abouters)
                .Die("mouseover.CssInfo")
                ;

            jQuery.FromObject(Document.Body)
                .Unbind("mousemove.CssInfo")
                ;

            new Await().AddDx(
                delegate
                {
                    if (_about != null)
                        _about.RemoveClass("Selector");
                    if (Element != null)
                        Element.Remove();
                    if (Btn != null)
                        Btn.RemoveClass("Toggled").RemoveAttr("disabled");
                }).Commit();
        }

        public static void Ev(jQueryEvent e, jQueryObject btn)
        {
            if (_instance == null)
            {
                _instance = new CssInfo();
                _instance.Btn = btn;
                btn.AddClass("Toggled");
            }
            else
            {
                _instance.Destroy();
            }
        }

        private const string MetaItem = "<div class='Item MetaControl'/>";

        private void MouseEnter(jQueryEvent e)
        {
            if (_about != null)
                _about.RemoveClass("Selector");
            else
                Element.Show();
            _about = jQuery.FromElement(e.Target);
            Element.Empty();

            _about.AddClass("Selector");

            jQueryPosition p = _about.GetOffset();

            //jQueryObject ls = jQuery.FromHtml("<div/>").AppendTo(Element);
            jQueryObject ls = Element;

            string id = _about.GetAttribute("id") != null ? "#" + _about.GetAttribute("id") : "";
            string cn = _about[0].TagName.ToUpperCase() + id;

            jQuery.FromHtml(MetaItem)
                .AppendTo(ls)
                .Html(cn)
                .CSS("font-weight", "bold")
                ;

            jQuery.FromHtml(MetaItem)
                .AppendTo(ls)
                .Html("Classes: " + _about.GetAttribute("class").Replace("Selector", ""))
                ;

            foreach (string n in CssProps)
            {
                jQuery.FromHtml(MetaItem)
                    .AppendTo(ls)
                    .Html(n + ": " + _about.GetCSS(n))
                    ;
            }
        }
    }
}